package cn.agiledata.bank.myspace.dao;

import java.util.List;

import cn.agiledata.bank.hibernateBean.PeerBankAccount;
import com.vnv.infrastructure.logic.IPBHibernate3BaseMgr;

public class PeerBankAccountDaoImpl extends IPBHibernate3BaseMgr implements
        PeerBankAccountDao {

    /**
     * 根据id得到银行联系人
     * 
     * @param id
     * @return
     */
    public PeerBankAccount getPeerBankAccount(Long id) {
        return (PeerBankAccount) getHibernateTemplate().get(
                PeerBankAccount.class, id);
    }

    /**
     * 根据当前用户id，银行卡号得到银行联系人
     * 
     * @param accountId
     * @param peerBankAccountId
     * @return
     */
    public PeerBankAccount getPeerBankAccount(Long accountId, String cardNo) {
        // TODO Auto-generated method stub
        StringBuffer sb = new StringBuffer();
        sb.append(" from PeerBankAccount p where 1=1 ");
        sb.append(" and p.account.id = ? ");
        sb.append(" and p.cardNo = ? ");

        List list = getHibernateTemplate().find(sb.toString(),
                new Object[] { accountId, cardNo });
        if (list == null) {
            return null;
        } else {
            if (list.size() == 0) {
                return null;
            } else {
                return (PeerBankAccount) list.get(0);
            }
        }
    }

    public PeerBankAccount getPeerBankAccount(String source, String accountId,
            String cardNo) {
        String hql = "from PeerBankAccount p where p.account.accountId=?"
                + " and p.cardNo=?";
        Object[] params = { accountId, cardNo };
        if (source != null && source.length() > 0) {
            hql += " and p.source=?";
            params = new Object[] { accountId, cardNo, source };
        }
        List list = getHibernateTemplate().find(hql, params);
        return list.size() == 0 ? null : (PeerBankAccount) list.get(0);
    }

    /**
     * 保存或者更新银行联系人
     * 
     * @param p
     */
    public void saveOrUpdatePeerBankAccount(PeerBankAccount p) {
        // TODO Auto-generated method stub
        getHibernateTemplate().saveOrUpdate(p);
    }

    /**
     * 删除银行联系人
     * 
     * @param p
     */
    public void deletePeerBankAccount(PeerBankAccount p) {
        // TODO Auto-generated method stub
        getHibernateTemplate().delete(p);
    }
}
